package com.ocom.human.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.dto.GetCustomRolePowerDto;
import com.ocom.common.entity.human.EntityRolePermission;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.company.companyrole.SetPageByRoleRequest;
import com.ocom.common.request.company.companyrole.SetPermissionDto;
import com.ocom.human.dao.IDaoRolePermission;
import com.ocom.human.service.RolePermissionService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RolePermissionServiceImpl extends ServiceImpl<IDaoRolePermission, EntityRolePermission> implements RolePermissionService {


    @Autowired
    private IDaoRolePermission iDaoRolePermission;


    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityRolePermission> selectRolePagePermission(Long comId, Integer role, Integer pageId) {
        if (role == -3) {
            return iDaoRolePermission.selectRolePagePermission(comId, role, pageId);
        } else {
            QueryWrapper<EntityRolePermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("api_id", 0);
            if (role >= 0) {
                queryWrapper.eq("role", role);
            } else     if (role == -2) {
                queryWrapper.lt("role", 10000);
            }
            if (pageId > 0) {
                queryWrapper.eq("page_id", pageId);
            }
            queryWrapper.orderByAsc("page_id", "role");
            return iDaoRolePermission.selectList(queryWrapper);
        }
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Boolean operationRolePagePermission(JSONObject jsonObject) {
        Integer pageId = jsonObject.getInteger("pageId");
        Integer buttonId = jsonObject.getInteger("buttonId");
        List<JSONObject> list = JSON.parseArray(JSON.toJSONString(jsonObject.get("permissionList")), JSONObject.class);
        for (JSONObject object : list) {
            QueryWrapper<EntityRolePermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("page_id", pageId);
            queryWrapper.eq("api_id", buttonId);
            queryWrapper.eq("role", object.getInteger("role"));
            queryWrapper.last(" limit 1");
            EntityRolePermission entityRolePermission = iDaoRolePermission.selectOne(queryWrapper);
            Date date = new Date();
            if (entityRolePermission == null) {
                EntityRolePermission entityRolePermissions = new EntityRolePermission();
                entityRolePermissions.setPageId(pageId);
                entityRolePermissions.setApiId(buttonId);
                entityRolePermissions.setRole(object.getInteger("role"));
                entityRolePermissions.setPermission(object.getByte("permission"));

                entityRolePermissions.setCreateTime(date);
                entityRolePermissions.setUpdateTime(date);
                this.save(entityRolePermissions);
            } else {
                entityRolePermission.setPermission(object.getByte("permission"));
                entityRolePermission.setUpdateTime(date);
                this.updateById(entityRolePermission);
            }
        }
        return true;
    }

    /**
     * @param role
     * @param comId
     * @param pageId
     * @return java.util.List<com.ocom.common.entity.human.EntityRolePermission>
     * @Description:获取角色按钮权限
     * @author lu
     * @date 2022/11/4 10:18
     */
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityRolePermission> getRoleButtonPermission(Integer role, Integer comId, Integer pageId) {
        //1、role 不等于 -3
        if (role == -3) {
            return iDaoRolePermission.getRoleButtonPermission(Long.valueOf(comId), role, pageId);
        } else {
            QueryWrapper<EntityRolePermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.gt("api_id", 0);
            if (role >= 0) {
                queryWrapper.eq("role", role);
            } else if (role == -2) {
                queryWrapper.lt("role", 10000);
            }
            if (pageId > 0) {
                queryWrapper.eq("page_id", pageId);
            }
            queryWrapper.orderByAsc("api_id", "role");
            return iDaoRolePermission.selectList(queryWrapper);
        }
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Boolean deleteWebButton(Integer pageId, Integer buttonId) {
        QueryWrapper<EntityRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("page_id", pageId);
        queryWrapper.eq("api_id", buttonId);
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Boolean deleteRolePermissionByRole(Integer role) {
        QueryWrapper<EntityRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", role);
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Boolean addRolePermission(SetPageByRoleRequest request) {
        Integer roleId = request.getRoleId();

        QueryWrapper<EntityRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", roleId);
        queryWrapper.gt("role", 10000L);
        this.remove(queryWrapper);

        List<EntityRolePermission> entitys = new ArrayList<>();
        Date now = new Date();
        for (SetPermissionDto dto : request.getPermissionList()) {
            EntityRolePermission entityRolePermissions = new EntityRolePermission();
            entityRolePermissions.setPageId(dto.getPageId());
            entityRolePermissions.setApiId(dto.getApiId());
            entityRolePermissions.setRole(roleId);
            entityRolePermissions.setPermission(dto.getPermission());
            entityRolePermissions.setCreateTime(now);
            entityRolePermissions.setUpdateTime(now);
            entitys.add(entityRolePermissions);
        }
        return this.saveBatch(entitys);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<GetCustomRolePowerDto> getCustomRolePower(Integer role, Integer comId) {
        return iDaoRolePermission.getCustomRolePower(role, comId);
    }
}
